package com.googlecode.spoluyan.google.googlebloggerdata;

import com.googlecode.spoluyan.google.common.AbstractGoogleAPIClient;
import com.googlecode.spoluyan.google.common.Defines;
import com.googlecode.spoluyan.google.common.Utils;
import com.googlecode.spoluyan.google.googlebloggerdata.data.Blog;
import com.googlecode.spoluyan.google.googlebloggerdata.data.BlogPages;
import com.googlecode.spoluyan.google.googlebloggerdata.data.BlogPosts;
import com.googlecode.spoluyan.google.googlebloggerdata.data.Comment;
import com.googlecode.spoluyan.google.googlebloggerdata.data.Page;
import com.googlecode.spoluyan.google.googlebloggerdata.data.Post;
import com.googlecode.spoluyan.google.googlebloggerdata.data.PostComments;
import com.googlecode.spoluyan.google.googlebloggerdata.data.User;
import com.googlecode.spoluyan.google.googlebloggerdata.data.UsersBlogs;

public class GoogleBloggerDataClient extends AbstractGoogleAPIClient {

    public GoogleBloggerDataClient(String key) {
	super(key);
    }

    public Blog getBlog(String blogId) {
	if (Utils.isEmptyString(blogId)) {
	    return null;
	}
	return Utils.fromJSON(Utils.doRequest(key, null, Defines.GoogleBloggerDataDefines.API_URL,
		Defines.GoogleBloggerDataDefines.Path.BLOGS, Defines.SLASH, blogId), Blog.class);
    }

    // TODO need OAuth token
    private UsersBlogs getUsersBlogs(String userId) {
	if (Utils.isEmptyString(userId)) {
	    return null;
	}
	return Utils.fromJSON(Utils.doRequest(key, null, Defines.GoogleBloggerDataDefines.API_URL,
		Defines.GoogleBloggerDataDefines.Path.USERS, userId, Defines.SLASH,
		Defines.GoogleBloggerDataDefines.Path.BLOGS), UsersBlogs.class);
    }

    public BlogPosts getPosts(String blogId) {
	if (Utils.isEmptyString(blogId)) {
	    return null;
	}
	return Utils.fromJSON(Utils.doRequest(key, null, Defines.GoogleBloggerDataDefines.API_URL,
		Defines.GoogleBloggerDataDefines.Path.BLOGS, Defines.SLASH, blogId, Defines.SLASH,
		Defines.GoogleBloggerDataDefines.Path.POSTS), BlogPosts.class);
    }

    public Post getPost(String blogId, String postId) {
	if (Utils.isEmptyString(blogId) || Utils.isEmptyString(postId)) {
	    return null;
	}
	return Utils.fromJSON(Utils.doRequest(key, null, Defines.GoogleBloggerDataDefines.API_URL,
		Defines.GoogleBloggerDataDefines.Path.BLOGS, Defines.SLASH, blogId, Defines.SLASH,
		Defines.GoogleBloggerDataDefines.Path.POSTS, Defines.SLASH, postId), Post.class);
    }

    public PostComments getComments(String blogId, String postId) {
	if (Utils.isEmptyString(blogId) || Utils.isEmptyString(postId)) {
	    return null;
	}
	return Utils.fromJSON(Utils.doRequest(key, null, Defines.GoogleBloggerDataDefines.API_URL,
		Defines.GoogleBloggerDataDefines.Path.BLOGS, Defines.SLASH, blogId, Defines.SLASH,
		Defines.GoogleBloggerDataDefines.Path.POSTS, Defines.SLASH, postId, Defines.SLASH,
		Defines.GoogleBloggerDataDefines.Path.COMMENTS), PostComments.class);
    }

    public Comment getComment(String blogId, String postId, String commentId) {
	if (Utils.isEmptyString(blogId) || Utils.isEmptyString(postId) || Utils.isEmptyString(commentId)) {
	    return null;
	}
	return Utils.fromJSON(Utils.doRequest(key, null, Defines.GoogleBloggerDataDefines.API_URL,
		Defines.GoogleBloggerDataDefines.Path.BLOGS, Defines.SLASH, blogId, Defines.SLASH,
		Defines.GoogleBloggerDataDefines.Path.POSTS, Defines.SLASH, postId, Defines.SLASH,
		Defines.GoogleBloggerDataDefines.Path.COMMENTS, Defines.SLASH, commentId), Comment.class);
    }

    public BlogPages getPages(String blogId) {
	if (Utils.isEmptyString(blogId)) {
	    return null;
	}
	return Utils.fromJSON(Utils.doRequest(key, null, Defines.GoogleBloggerDataDefines.API_URL,
		Defines.GoogleBloggerDataDefines.Path.BLOGS, Defines.SLASH, blogId, Defines.SLASH,
		Defines.GoogleBloggerDataDefines.Path.PAGES), BlogPages.class);
    }

    public Page getPage(String blogId, String pageId) {
	if (Utils.isEmptyString(blogId) || Utils.isEmptyString(pageId)) {
	    return null;
	}
	return Utils.fromJSON(Utils.doRequest(key, null, Defines.GoogleBloggerDataDefines.API_URL,
		Defines.GoogleBloggerDataDefines.Path.BLOGS, Defines.SLASH, blogId, Defines.SLASH,
		Defines.GoogleBloggerDataDefines.Path.PAGES, Defines.SLASH, pageId), Page.class);
    }

    // TODO need OAuth token
    private User getUser(String userId) {
	if (Utils.isEmptyString(userId)) {
	    return null;
	}
	return Utils.fromJSON(Utils.doRequest(key, null, Defines.GoogleBloggerDataDefines.API_URL,
		Defines.GoogleBloggerDataDefines.Path.USERS, userId), User.class);
    }
}
